import java.util.ArrayList;
import java.util.Random;

public class GenerateBoard {

    int[][] board = new int[9][9];
    ArrayList<Integer> number = new ArrayList<>();

    //生成可操作的ArrayList
    private  void settingArray(){
        for(int i=1;i<=9;i++) number.add(i);
    }

    //生成第一行
    private void generateFirstRow(){
        Random rd = new Random();
        for(int i=0;i<9;i++){
            board[0][i] = number.get(rd.nextInt(number.size()));
            number.remove((Integer) board[0][i]);
        }
        //必须要先清空number，不然会产生上一轮循环的冗余
        number.clear();
        settingArray();
    }

    //生成第一列
    private void generateFirstCol(){
        number.remove((Integer) board[0][0]);
        number.remove((Integer) board[0][1]);
        number.remove((Integer) board[0][2]);
        for(int i=1;i<=2;i++) {
            int j = number.size();
            board[i][0] = number.get(new Random().nextInt(j));
            number.remove((Integer) board[i][0]);
        }
        number.clear();
        settingArray();
        number.remove((Integer) board[0][0]);
        number.remove((Integer) board[1][0]);
        number.remove((Integer) board[2][0]);

        for(int i=3;i<9;i++){
            int j = number.size();
            if(j==1) board[i][0] = number.get(0);
            else
            {
                board[i][0] = number.get(new Random().nextInt(j));
                number.remove((Integer) board[i][0]);
            }
        }
        number.clear();
        settingArray();
    }

    //替换为字符数组
    private char[][] generateCharBoard(char[][] charBoard){
        for(int i=0;i<9;i++){
            for(int j=0;j<9;j++){
                charBoard[i][j] = (char) (board[i][j]+'0');
            }
        }
        return charBoard;
    }

    private void solveSudoku(char[][] board) {
        solveSudokuHelper(board);
    }

    //回溯法解数独
    private boolean solveSudokuHelper(char[][] board){
        for (int i = 0; i < 9; i++){
            for (int j = 0; j < 9; j++){
                if (board[i][j] != '0'){
                    continue;
                }
                for (char k = '1'; k <= '9'; k++){
                    if (isValidSudoku(i, j, k, board)){
                        board[i][j] = k;
                        if (solveSudokuHelper(board)){
                            return true;
                        }
                        board[i][j] = '0';
                    }
                }
                return false;
            }
        }
        return true;
    }

    private boolean isValidSudoku(int row, int col, char val, char[][] board){
        for (int i = 0; i < 9; i++){
            if (board[row][i] == val){
                return false;
            }
        }

        for (int j = 0; j < 9; j++){
            if (board[j][col] == val){
                return false;
            }
        }

        int startRow = (row / 3) * 3;
        int startCol = (col / 3) * 3;
        for (int i = startRow; i < startRow + 3; i++){
            for (int j = startCol; j < startCol + 3; j++){
                if (board[i][j] == val){
                    return false;
                }
            }
        }
        return true;
    }

    public int[][] generation(){
        char[][] charBoard= new char[9][9];
        settingArray();
        generateFirstRow();
        generateFirstCol();
        solveSudoku(generateCharBoard(charBoard));
        int[][] finalBoard = new int[9][9];
        for(int i=0;i<9;i++){
            for(int j=0;j<9;j++){
                finalBoard[i][j] = charBoard[i][j]-'0';
            }
        }
        return finalBoard;
    }

    public static void main(String[] args){
        int[][] board = new int[9][9];
        GenerateBoard generateBoard = new GenerateBoard();
        board = generateBoard.generation();
        for(int i=0;i<9;i++){
            for(int j=0;j<9;j++){
                System.out.print(board[i][j]+" ");
                if(j==8){
                    System.out.println();
                }
            }
        }
    }}
